package com.example.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.Result;
import com.example.entity.Volunteer;
import com.example.service.VolunteerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Tag(name = "志愿者管理")
@RestController
@RequestMapping("/api/volunteer")
@RequiredArgsConstructor
public class VolunteerController {

    private final VolunteerService volunteerService;

    @Operation(summary = "查询志愿者信息")
    @GetMapping("/{id}")
    public Result<Volunteer> getVolunteerInfo(
            @Parameter(description = "志愿者ID") @PathVariable Long id) {
        return Result.success(volunteerService.getById(id));
    }

    @Operation(summary = "分页查询志愿者列表")
    @GetMapping("/list")
    public Result<Map<String, Object>> getVolunteerList(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer pageSize,
            @Parameter(description = "搜索关键词") @RequestParam(required = false) String keyword,
            @Parameter(description = "状态：0-待审核 1-正常 2-禁用") @RequestParam(required = false) Integer status) {
        
        Page<Volunteer> pageResult = volunteerService.getVolunteerList(pageNum, pageSize, keyword, status);
        Map<String, Object> result = new HashMap<>();
        result.put("total", pageResult.getTotal());
        result.put("list", pageResult.getRecords());
        return Result.success(result);
    }

    @Operation(summary = "添加志愿者")
    @PostMapping
    public Result<Boolean> addVolunteer(@RequestBody Volunteer volunteer) {
        return Result.success(volunteerService.addVolunteer(volunteer));
    }

    @Operation(summary = "更新志愿者信息")
    @PutMapping
    public Result<Boolean> updateVolunteer(@RequestBody Volunteer volunteer) {
        return Result.success(volunteerService.updateVolunteer(volunteer));
    }

    @Operation(summary = "删除志愿者")
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteVolunteer(@PathVariable Long id) {
        return Result.success(volunteerService.deleteVolunteer(id));
    }

    @Operation(summary = "批量删除志愿者")
    @DeleteMapping("/batch")
    public Result<Boolean> batchDeleteVolunteers(@RequestBody Long[] ids) {
        return Result.success(volunteerService.batchDeleteVolunteers(ids));
    }

    @Operation(summary = "更新志愿者状态")
    @PutMapping("/status")
    public Result<Boolean> updateVolunteerStatus(@RequestBody Map<String, Object> params) {
        Long id = Long.valueOf(params.get("id").toString());
        Integer status = Integer.valueOf(params.get("status").toString());
        return Result.success(volunteerService.updateVolunteerStatus(id, status));
    }
} 